package com.sinosoft.web.easyui.api;

import cn.com.sinux.spring.commons.PropertyCollection;
import com.sinosoft.hydra.um.condition.SearchPBomCondition;
import com.sinosoft.hydra.um.domain.model.DirectoryType;
import com.sinosoft.hydra.um.domain.permission.TPBom;
import cn.com.sinux.spring.exception.BusinessException;
import com.sinosoft.hydra.um.helper.LoggerHelper;
import com.sinosoft.hydra.um.services.DirectoryService;
import com.sinosoft.hydra.um.services.SysPBomService;
import cn.com.sinux.spring.utils.PageUtils;
import com.sinosoft.web.common.WebInput;
import cn.com.sinux.spring.json.JsonFilter;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2016/6/30.
 */
@Controller
@RequestMapping("/api/pbom")
public class SysPBomApi {

    @Autowired
    private SysPBomService pBomService;
    @Autowired
    private DirectoryService directoryService;

    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping("index")
    @RequiresPermissions("pbom:data:list")
    public String index(Model model) {
        LoggerHelper.logInfo("访问了设计结构列表信息");
        return "hydra_easyui/pbom/index";
    }

    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping("toAdd")
    @RequiresPermissions("pbom:data:add")
    public String toAdd(Model model) {
        model.addAttribute("bomTypes", directoryService.findDirectories(DirectoryType.valueOf("DIRECTORY_PBOM_TYPE")));
        return "hydra_easyui/pbom/edit";
    }

    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping("addSingle")
    @RequiresPermissions("pbom:data:add")
    public String addSingle(Model model) {
        model.addAttribute("bomTypes", directoryService.findDirectories(DirectoryType.valueOf("DIRECTORY_PBOM_TYPE")));
        return "hydra_easyui/pbom/add";
    }


    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping("choose")
    @RequiresPermissions("pbom:data:add")
    public String choose(Model model) {
        return "hydra_easyui/pbom/choose";
    }


    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping("toEdit")
    @RequiresPermissions(logical = Logical.OR, value = {"pbom:data:add", "pbom:data:update"})
    public String toUpdate(Model model, @RequestParam(value = "id", required = true) String id) {
        model.addAttribute("pbom", pBomService.findOne(id));
        return "hydra_easyui/pbom/edit";
    }

    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping("toSearch")
    @RequiresPermissions("pbom:data:list")
    public String search(Model model) {
        return "hydra_easyui/pbom/search";
    }

    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping("view")
    @RequiresPermissions("pbom:data:list")
    public String view(Model model, @RequestParam(value = "id", required = true) String id) {
        model.addAttribute("id", id);
        return "hydra_easyui/pbom/view";
    }


    /**
     * 发布结构
     *
     * @return 跳转制定也
     */
    @RequestMapping(value = "publish", method = RequestMethod.POST)
    @RequiresPermissions(logical = Logical.OR, value = {"pbom:data:add", "pbom:data:update"})
    @ResponseBody
    public Object publish(Model model, @RequestParam(value = "id", required = true) String id) {
        List<TPBom> tpBoms = pBomService.publish(id);
        LoggerHelper.logInfo("发布了设计结构{}", new LoggerHelper.PropertyReader<TPBom>(tpBoms, "pName"));
        return JsonFilter.doPropertyFilter(tpBoms, new PropertyCollection()
                .addProperties(TPBom.class, "id"));
    }

    /**
     * 新增顶层节点
     *
     * @return 跳转制定也
     */
    @RequestMapping(value = "addTop", method = RequestMethod.POST)
    @RequiresPermissions("pbom:data:add")
    @ResponseBody
    public Object addTop(Model model, TPBom tpBom) {
        TPBom result = pBomService.addTop(tpBom);
        LoggerHelper.logInfo("添加了设计结构的顶层结构{}", result.getpName());
        return JsonFilter.doPropertyFilter(result, new PropertyCollection()
                .addProperties(TPBom.class, "id"));
    }

    /**
     * 发布结构
     *
     * @return 跳转制定也
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    @RequiresPermissions(logical = Logical.OR, value = {"pbom:data:add", "pbom:data:update"})
    @ResponseBody
    public Object save(Model model, TPBom tpBom, @RequestParam(value = "publish", required = false, defaultValue = "false") boolean publish) {
        List<TPBom> tpBoms = pBomService.savePBom(tpBom, publish);
        LoggerHelper.logInfo("保存了设计结构的结构信息{}", new LoggerHelper.PropertyReader<TPBom>(tpBoms, "pName"));
        return JsonFilter.doPropertyFilter(tpBoms, new PropertyCollection()
                .addProperties(TPBom.class, "id"));
    }

    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping("createCode")
    @RequiresPermissions("pbom:data:add")
    @ResponseBody
    public Object createCode(
            @RequestParam(value = "pName", required = true) String pName) {
        return Collections.singletonMap("code", pBomService.createCode(pName));
    }

    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping("list")
    @RequiresPermissions("pbom:data:list")
    @ResponseBody
    public Object list(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNumber,
            @RequestParam(value = "rows", required = false, defaultValue = "10") Integer pageSize,
            SearchPBomCondition pBomCondition) {
        Pageable pageable = PageUtils.buildPageRequest(pageNumber, pageSize, "id", "desc");
        Page<TPBom> page = pBomService.findPBoms(pBomCondition, pageable);
        return JsonFilter.doPropertyFilter(page, new PropertyCollection()
                .addProperties(TPBom.class, "id", "code", "pName", "number", "bomType", "status", "suitAll", "nonBuild", "state", "canBuild"));
    }

    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping("children")
    @RequiresPermissions("pbom:data:list")
    @ResponseBody
    public Object list(@RequestParam(value = "id", required = true) String id) {
        List<TPBom> page = pBomService.findChildren(id);
        return JsonFilter.doPropertyFilter(page, new PropertyCollection()
                .addProperties(TPBom.class, "id", "code", "pName", "number", "bomType", "suitAll", "nonBuild", "status", "state", "canBuild"));
    }

    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping(value = "deletePBom", method = RequestMethod.POST)
    @RequiresPermissions("pbom:data:delete")
    @ResponseBody
    public Object deletePBom(@RequestParam(value = "id", required = true) String id) {
        TPBom pbom = pBomService.delPBom(id);
        LoggerHelper.logInfo("删除了设计结构{}", pbom.getpName());
        return JsonFilter.doPropertyFilter(pbom, new PropertyCollection()
                .addProperties(TPBom.class, "id", "code", "pName", "status"));
    }

    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping("export")
    @RequiresPermissions("pbom:data:export")
    public void export(
            WebInput in, HttpServletResponse response,
            @RequestParam(value = "id", required = true) String id,
            @RequestParam(value = "hasCode", required = false, defaultValue = "true") boolean hasCode) {
        try {
            TPBom pbom = pBomService.findOne(id);
            byte[] bytes = pBomService.doExportPBom(id, hasCode);
            LoggerHelper.logInfo("导出了设计结构“{}”结构数据", pbom.getpName());
            in.getWebOutput(response).sendFile(bytes, pbom.getpName() + "的设计结构.xls", null, true, 30, true, false);
        } catch (IOException ex) {
            throw new BusinessException("结构导出失败");
        }
    }
}
